package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Kirito
 * @description:
 * @date 2020/04/11
 */
@Controller
@RequestMapping("resumes")
public class ResumeController {

    @Autowired
    private IResumeService resumeService;

    /**
     * 查询简历列表
     *
     * @return Result
     * @throws Exception Exception
     */
    @GetMapping("/allResume")
    public String findAllResumes(Model model) throws Exception {
        List<Resume> resumes = resumeService.findAll();
        model.addAttribute("data",resumes);
        return "resume";
    }

    @RequestMapping("/toAddResume")
    public String toAddPaper() {
        return "add";
    }

    /**
     * 新增简历
     *
     * @param resume Resume
     * @return Result
     * @throws Exception Exception
     */
    @PostMapping("/addResume")
    public String saveResume(Resume resume) throws Exception {
        boolean success = resumeService.save(resume);
        return "redirect:/resumes/allResume";
    }

    /**
     * 跳转修改简历页面
     *
     * @param id Long
     * @return Result
     * @throws Exception Exception
     */
    @GetMapping("/update/{id}")
    public String toUpdate(@PathVariable(value = "id") Long id, Model model) throws Exception {
        Resume resume = resumeService.findById(id);
        model.addAttribute("resume", resume);
        return "update";
    }

    /**
     * 处理修改简历请求
     * @param model
     * @param resume
     * @return
     * @throws Exception
     */
    @PostMapping("update")
    public String updateResume(Model model, Resume resume) throws Exception {
        resumeService.update(resume);
        return "redirect:/resumes/allResume";
    }

    /**
     * 删除简历
     *
     * @param id Long
     * @return Result
     * @throws Exception Exception
     */
    @GetMapping("/delete/{id}")
    public String deleteResume(@PathVariable(value = "id") Long id) throws Exception {
        resumeService.deleteById(id);
        return "redirect:/resumes/allResume";
    }

}
